<?php
namespace app\admin\controller\manager;

use app\admin\controller\Controller;
use app\admin\model\AdminOperationLog as AdminOperationLogModel;
use think\api\Client;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Env;

class Index extends Controller
{

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
    }
    // 环境信息
    public function index(){
        // 配置返回参数
        $info = [
           'Weather'=>getWeather(),
           'operation_log'=>Env("OPERATION_LOG.OPERATION_LOG")?(new AdminOperationLogModel)->getAll():[],
           'new_users'=>100,
           'sales'=>100.00,
           'new_order'=>98,
           'total_profit'=>98421.79, 
        ];
        // 返回
        return $this->renderSuccess($info);
    }

    // 今日收入
    public function todaySales(){
        $data = [
            ['time'=>'0:00','value'=>89],
            ['time'=>'4:00','value'=>89],
            ['time'=>'8:00','value'=>18],
            ['time'=>'12:00','value'=>78],
            ['time'=>'16:00','value'=>498],
            ['time'=>'20:00','value'=>123],
            ['time'=>'24:00','value'=>309]
        ];
        $total = number_format(3698,2);
        return $this->renderSuccess(compact('data','total'));
    }

    // 月收入
    public function monthSales(){
        // 获取本月初到今天的时间
        $data = Db::query("SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d') as date,sum(active_number) as active_number FROM `tvm_active_log` WHERE create_time >".strtotime(date('Y-m')."-01")." GROUP BY  date;");
        if(count($data) < 30)
        {
            $day = date('d',strtotime(end($data)['date']));
            $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
            for ($i= $day; $i < date('d', strtotime("$BeginDate +1 month -1 day")); ++$i) { 
                # code...
                $num = $i  + 1;
                $data[$i] = [
                    'date'=>date('Y-m-').sprintf("%02d", $num),
                    'active_number'=> "0"
                ];
            }
        }
        $data = array_values($data);
        $total = number_format(Db::name('active_log')->where('create_time >'.strtotime(date('Y-m')."-01"))->sum('active_number'),2);
        return $this->renderSuccess(compact('data','total'));
    }

    // 用户活跃度
    public function activeUsers(){
        // 获取本月初到今天的时间
        $data =  Db::query("SELECT FROM_UNIXTIME(create_time,'%Y-%m') as date,sum(active_number) as active_number FROM `tvm_active_log` WHERE create_time >".strtotime(date('Y')."-01-01")." GROUP BY  date;");
        if(count($data) < 12)
        {
            $day = date('m',strtotime(end($data)['date']));
            for ($i= $day; $i < 12; ++$i) { 
                # code...
                $num = $i  + 1;
                $data[$i] = [
                    'date'=>date('Y-').sprintf("%02d", $num),
                    'active_number'=> "0"
                ];
            }
        }
        return $this->renderSuccess(array_values($data));
    }

}